package cn.kimming.wms.web.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import cn.kimming.wms.anno.RequiredPermission;
import cn.kimming.wms.domain.JsonMsg;
import cn.kimming.wms.domain.StockOutcomeBill;
import cn.kimming.wms.excep.StockIsNotEnoughException;
import cn.kimming.wms.query.StockOutcomeBillQueryObject;
import cn.kimming.wms.service.IClientService;
import cn.kimming.wms.service.IDepotService;
import cn.kimming.wms.service.IStockOutcomeBillService;
import lombok.Getter;

@Controller("stockOutcomeBillAction")
@Scope("prototype")
public class StockOutcomeBillAction extends BaseAction{

	private static final long serialVersionUID = 26674362125140616L;

	@Autowired
	private IStockOutcomeBillService stockOutcomeBillService;
	@Autowired
	private IDepotService depotService;
	@Autowired
	private IClientService clientService;

	@Getter
	private StockOutcomeBill stockOutcomeBill = new StockOutcomeBill();

	@Getter
	private StockOutcomeBillQueryObject qo = new StockOutcomeBillQueryObject();

	@Override
	@RequiredPermission("销售出库订单列表")
	@InputConfig(methodName="input")
	public String execute() {
		try {
			ActionContext.getContext().put("objs", stockOutcomeBillService.query(qo));
			ActionContext.getContext().put("depots", depotService.list());
			ActionContext.getContext().put("clients", clientService.list());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LIST;
	}
	
	@RequiredPermission("销售出库订单查看")
	public String show() {
		try {
			if (stockOutcomeBill.getId() != null) {
				stockOutcomeBill = stockOutcomeBillService.get(stockOutcomeBill.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}

	
	@RequiredPermission("销售出库订单编辑")
	public String input() throws Exception {
		ActionContext.getContext().put("depots", depotService.list());
		ActionContext.getContext().put("clients", clientService.list());
		if (stockOutcomeBill.getId() != null) {
			stockOutcomeBill = stockOutcomeBillService.get(stockOutcomeBill.getId());
		}
		return INPUT;
	}
	
	@RequiredPermission("销售出库订单保存和更新")
	public String saveOrUpdate() {
		if (stockOutcomeBill.getId() != null) {
			try {
				stockOutcomeBillService.update(stockOutcomeBill);
				addActionMessage("更新成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,更新失败!");
			}
		} else {
			try {
				stockOutcomeBillService.save(stockOutcomeBill);
				addActionMessage("保存成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,保存失败!");
			}
		}
		return SUCCESS;
	}

	
	
	@RequiredPermission("销售出库订单删除")
	public String delete() {
		try {
			if (stockOutcomeBill.getId() != null) {
				stockOutcomeBillService.delete(stockOutcomeBill.getId());
				putJSON(new JsonMsg(true, "删除成功"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,删除失败!"));
		}
		return NONE;
	}
	
	@RequiredPermission("销售出库订单审核")
	public String audit() {
		try {
			if (stockOutcomeBill.getId() != null) {
				stockOutcomeBillService.audit(stockOutcomeBill.getId());
				putJSON(new JsonMsg(true, "审核成功"));
			}
		} catch (NullPointerException e) {
			putJSON(new JsonMsg(false, e.getMessage()));
		} catch (StockIsNotEnoughException e) {
			putJSON(new JsonMsg(false, e.getMessage()));
		}catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,审核失败!"));
		}
		return NONE;
	}
	
	public void prepareSaveOrUpdate() throws Exception {
		if (stockOutcomeBill.getId()!= null) {
			stockOutcomeBill = stockOutcomeBillService.get(stockOutcomeBill.getId());
			stockOutcomeBill.setDepot(null);
			stockOutcomeBill.setClient(null);
		}
		stockOutcomeBill.getItems().clear();
	}
	
	
}
